using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ClientLogic;
using MsgLogic;
using System.Drawing;

namespace TestConsoleClient
{
    class Program
    {
        //Client client = null;
        static void Main(string[] args)
        {
            string choice;
            
            Random r = new Random();
            string username = "Akiva" + r.Next().ToString();

            Client client = new Client(IPAddress.Parse("127.0.0.1"), 100, username, "black");
            client.MsgArrived += new EventHandler<MsgArrivedEventArgs>(client_MsgArrived);
            client.ServerDisconnected += new EventHandler<ServerDisconnectedEventArgs>(client_ServerDisconnected);
            client.Connect();
            client.SendMsg(new StartMsg(username));

            while (true)
            {
                Console.WriteLine("please choose option");
                Console.WriteLine("1. connect client to server");
                Console.WriteLine("2. send message");
                Console.WriteLine("3. disconnect client");
                Console.WriteLine("4. exit");

                choice = Console.ReadLine();
               
                switch (choice)
                {
                    case "1":
                        client = new Client(IPAddress.Parse("127.0.0.1"), 100, username, "black");
                        client.MsgArrived += new EventHandler<MsgArrivedEventArgs>(client_MsgArrived);
                        client.ServerDisconnected += new EventHandler<ServerDisconnectedEventArgs>(client_ServerDisconnected);
                        client.Connect();
                        client.SendMsg(new StartMsg(username));
                        //client.SendMsg(new TextMsg("Akiva Brun", "hello world", Color.Red));
                        //Console.ReadLine();
                        break;
                    case "2":
                        client.SendMsg(new TextMsg(username, "hello world", Color.Red));
                        break;
                    case "3":
                        client.SendMsg(new DisconnectMsg(username));
                        break;
                    case "4":
                        return;

                }
            }

        }

        static void client_ServerDisconnected(object sender, ServerDisconnectedEventArgs e)
        {
            Console.WriteLine("The server disconnected");
        }

        static void client_MsgArrived(object sender, MsgArrivedEventArgs e)
        {
            if (e.Msg as StartMsg != null)
            {
                Console.WriteLine(e.Msg.UserName + " has just connected");
            }
            else if (e.Msg as InfoMsg != null)
            {
                //TODO: info msg
                Console.WriteLine(((TextMsg)e.Msg).Txt);
            }
            else   //disconnect
            {
                if (e.Msg.UserName == "Server")
                {
                    client_ServerDisconnected(null, null);
                }
                else
                {
                    Console.WriteLine(e.Msg.UserName + " has just disconnected");
                }
            }
        }
    }
}
